<template>
	<div class="bgBox">
		<div class="container">
			<div class="userBox" v-if="avatarUrl">
				<div class="imgBox"><u--image :src="avatarUrl" shape="circle" width="40" height="40"></u--image></div>
				<div class="nickname">{{ nickName }} </div>
			</div>
			
			<div class="list">
				<div class="item" @click="showmy = true"><u-icon name="tags"></u-icon><span>我发起的</span></div>
				<div class="item" @click="showcy = true"><u-icon name="chat"></u-icon><span>我参加的</span></div>
			</div>
		</div>
		
		<u-popup :show="showmy" mode="center" @close="showmy = false" >
			<view class="mycontent">
				<div class="myList">
					<h2 class="textTlt">排序</h2>
					<div class="overBox">
						<div class="item" v-for="(item,index) in paixuList" @click="toDetail('paixu_join',item._id)">
							<h4>{{item.zhuti}}</h4>
							<time>{{item.time}}</time>
						</div>
					</div>
				</div>
			</view>
		</u-popup>
		<u-popup :show="showcy" mode="center" @close="showcy = false" >
			<view class="mycontent">
				<div class="myList">
					<div class="overBox">
						<div class="item" v-for="(item,index) in cyList" @click="toDetail(item.hd,item.wyz)">
							<h4 class="cytlt">{{index + 1}}、{{item.zhuti}}</h4>
						</div>
					</div>
				</div>
			</view>
		</u-popup>
		
		<u-toast ref="uToast"></u-toast>
	</div>
</template>

<script>
const db = wx.cloud.database();
export default {
	name: 'headcomd',
	data() {
		return {
			avatarUrl: 'https://mp.weixin.qq.com/wxopen/basicprofile?action=get_headimg&token=738756683&t=1677487093879',
			nickName: '微信用户',
			openid: '',
			showmy: false,
			paixuList: [],
			fenzuList: [],
			showcy: false,
			cyList: []
			
		};
	},
	
	onLoad(options) {
		if (!options) {
			this.infoScene = 'default';
		} else {
			
		}
	},
	computed: {},
	mounted() {
		
	},
	watch: {},
	created() {
		let that = this;
		var openid = wx.getStorageSync('useropenid');
		var user_ava_nick = wx.getStorageSync('user_ava_nick');
		if(user_ava_nick){
			that.avatarUrl = user_ava_nick.avatarUrl
			that.nickName = user_ava_nick.nickName
		}
		if (openid) {
			that.openid = openid;
			that.getMyfq()
			that.getMycy()
		} else {
			that.getopenid();
		}
	},
	methods: {
		getopenid() {
			let that = this;
			wx.cloud.callFunction({
				name: 'getopenid',
				data: {},
				complete: res => {
					console.log('callFunction result: ', res);
					that.saveOpenid(res.result.openid);
					that.openid = res.result.openid
					wx.setStorageSync('useropenid', res.result.openid);
					
					that.getMyfq()
					that.getMycy()
					
				}
			});
		},
		saveOpenid(openid) {
			db.collection('user_info')
				.where({
					openid: openid
				})
				.get({
					success: function(res) {
						console.log('success: ', res);
						if (res.data.length == 0) {
							db.collection('user_info').add({
								data: {
									openid: openid,
									time: new Date()
								},
								success: function(res) {
									console.log('openid储存成功' + openid);
								}
							});
						}
					}
				});
		},
		getMyfq(){
			let that = this
			db.collection('paixu_danpai')
				.where({
					_openid: that.openid
				})
				.get({
					success: function(res) {
						if (res.data.length >= 0) {
							that.paixuList = res.data
						}
					}
				});
		},
		getMycy(){
			let that = this
			db.collection('paixu_danpai_cyyh')
				.where({
					openid: that.openid
				})
				.get({
					success: function(res) {
						console.log(res)
						if (res.data.length >= 0) {
							that.cyList = res.data[0].cyhd
							
						}
					}
				});
		},
		toDetail(path,wyz){
			uni.navigateTo({
				url: "./" + path + "?wyz=" + wyz
			})
		}
		
	}
};
</script>

<style>
@import url(./index.css);
</style>
